const { toUrl } = getApp();
import { request } from "@/utils/request";
Page({
  toUrl,
  show() {
    this.setData({
      show: !this.data.show,
    });
  },
  data: {
    show: false,
    current: 0,bannerList:[],
    cardList: [
      { title: "我的车辆", icon: "/img/car1.png", url: "./car/list" },
      { title: "缴费记录", icon: "/img/car2.png", url: "./pay/list" },
      { title: "停车券", icon: "/img/car3.png", url: "./ticket/ticket" },
      {
        title: "停车规则",
        icon: "/img/car4.png",
        url: "/pages/login/agreement/agreement?title=停车规则",
      },
    ],
    carList: {},
    chepai: [],
    tabAvtive:false
  },
  handleCarNum(){
    wx.chooseLicensePlate({
        success:(e)=>{
            console.log(e)
            if (e.errMsg == 'chooseLicensePlate:ok') {
                this.ksCar(e.plateNumber)
            }
        }
    })
},
  //获取banner
  banner(){
    let data = {
      typeid: 4,
    };
    request("index/bannerlist", { data }).then((res) => {
      if (res.errcode == 1) {
        console.log(res);
        this.setData({
          bannerList:res.data
        });
      } else {
        wx.showToast({
          title: res.message,
          icon:'none'
        });
      }
    });
  },
  //查询停车费用
  selectCar(carnum = '') {
    // this.data.show = true
    // this.setData({
    //   show:true
    // })
    // return
    let carNum;
    const carAlert = this.selectComponent("#carAlert");
    if (carnum.type != undefined) {
      console.log(111);
      carNum = Number(carAlert.data.value.join("").length) + Number(carAlert.data.value1.join("").length);
      console.log(carNum);
      if (carNum < 7) {
        wx.showToast({
          title: "请输入车牌号",
          icon: "error",
        });
        return;
      }

      console.log(carAlert.data.value.join(""),carAlert.data.value1.join(""));
      carNum = carAlert.data.value.join("") +''+ carAlert.data.value1.join("");
    } else {
      carNum = carnum;
      let copyCarNum = carnum
      let value = []
      let value1 = []
      copyCarNum = copyCarNum.toString().split('')
      console.log(copyCarNum.length);
      copyCarNum.forEach((it,idx)=>{
        if (idx < 2) {
          value[idx] = it
        }else{
          value1[idx - 2] = it
        }
      })
      if (copyCarNum.length >= 8) {
        const carAlert = this.selectComponent("#carAlert");
        carAlert.setData({
          titleAfter:true
        })
      }
      carAlert.setData({
        value1,
        value,
      })
    }
    let data = {
      // plateNo: "闽AAA302", // carAlert.data.value
      plateNo: carNum,
    };
    wx.showLoading({
      mask:true
    });
    request("Carapi/getParkingPaymentInfo", { data }).then((res) => {
      wx.hideLoading();
      if (res.errcode == 1) {
        this.setData({
          show: !this.data.show,
          carList: res.data,
        });
      } else {
        wx.showToast({
          title: res.message,
        });
      }
    });
  },

  ksCar(e) {
    let carnum = ''
    if (e.currentTarget != undefined) {
      carnum = e.currentTarget.dataset.carnum
    }else{
      carnum = e
    }
    this.selectCar(carnum)
  },
  changeSwiper({ detail }) {
    const { current } = detail;
    this.setData({ current });
  },
  myCarList() {
    wx.showLoading();
    request("Userscar/carlist").then((res) => {
      wx.hideLoading();
      if (res.errcode == 1) {
        this.setData({
          chepai: res.data,
        });
      } else {
        wx.showToast({
          title: res.message,
        });
      }
    });
  },
  onLoad(options) {
    this.banner()
  },
  conmitTag(data){
    console.log(data.detail);
    this.setData({
      tabAvtive:data.detail
    })
  },
  onShow() {
    const carAlert = this.selectComponent("#carAlert");
    carAlert.setData({
      value1:[],
      value:[],
      endIndex:0
    })
    this.setData({
      show:false
    })
  },
});
